package test;

import com.cn.zpark.utils.DruidUtils;
import com.sun.org.apache.regexp.internal.RE;

import java.sql.Connection;
import java.sql.Statement;

/**
 * @PackageName: test
 * @ClassName: BatchInsertUser
 * @Description:
 * @author: RZS
 * @date: 2022/4/22  19:32
 */
public class BatchInsertUser {
    public static void main(String[] args) {
        // 准备连接信息
        Connection conn = null;
        Statement stat = null;

        try {
            // 获取连接对象
            conn = DruidUtils.getConn();
            // 创建sql执行对象
            stat = conn.createStatement();
            // 创建sql语句
            for (int i = 1; i <= 100000; i++) {

                // 生成的话号码
                String phone = System.currentTimeMillis() + "";
                phone = phone.substring(0, 11);
                phone = Long.parseLong(phone) + i + "";

                String sql = "insert into user(username, phone, email, salary) " +
                        "values('龙霸天"+i+"', "+phone+", 'lbt"+i+"@163.com', "+(i + 10.0)+")";

                // 添加到批量处理
                stat.addBatch(sql);
            }
            // 执行sql
            stat.executeBatch();
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            DruidUtils.close(conn, null, stat);
        }


    }
}
